//
//  JZMTBtnView.m
//  Created by QA on 15/6/30.
//  Copyright (c) 2015年 QA. All rights reserved.
//

#import "JZMTBtnView.h"
#import <SDWebImage/UIImageView+WebCache.h>
#import "UIColorDispose.h"
#import "CustomBadge.h"

@implementation JZMTBtnView
@synthesize titleLable;

/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/

-(id)initWithFrame:(CGRect)frame title:(NSString *)title imageStr:(NSString *)imageStr
{
    self = [super initWithFrame:frame];
    if (self) {
        UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(frame.size.width/2-18, 15, 36, 36)];
        imageView.contentMode = UIViewContentModeRedraw;
        [imageView sd_setImageWithURL:[NSURL URLWithString:imageStr] placeholderImage:[UIImage new]];
        [self addSubview:imageView];
        
        self.titleLable = [[UILabel alloc] initWithFrame:CGRectMake(0, 17+38, frame.size.width, 20)];
        titleLable.text = title;
        titleLable.textAlignment = NSTextAlignmentCenter;
        titleLable.font = [UIFont systemFontOfSize:13];
        titleLable.textColor = [UIColorDispose colorWithHexString:@"505050"];
        [self addSubview:titleLable];
        
        UIView *markView = [[UIView alloc] initWithFrame:CGRectMake(0, frame.size.width - 20, 5,5)];
        markView.backgroundColor = [UIColor redColor];
        [markView.layer setCornerRadius:2.5];//圆角
        [self addSubview:markView];
        
    }
    return self;
}

-(id)initWithFrame:(CGRect)frame title:(NSString *)title imageStr:(NSString *)imageStr markNumber:(NSInteger)markCount
{
    self = [super initWithFrame:frame];
    if (self) {
        UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(frame.size.width/2-18, 15, 36, 36)];
        imageView.contentMode = UIViewContentModeRedraw;
        [imageView sd_setImageWithURL:[NSURL URLWithString:imageStr] placeholderImage:[UIImage new]];
        [self addSubview:imageView];
        
        self.titleLable = [[UILabel alloc] initWithFrame:CGRectMake(0, 17+38, frame.size.width, 20)];
        titleLable.text = title;
        titleLable.textAlignment = NSTextAlignmentCenter;
        titleLable.font = [UIFont systemFontOfSize:13];
        titleLable.textColor = [UIColorDispose colorWithHexString:@"505050"];
        [self addSubview:titleLable];
        
        if(markCount > 0){
            UIView *markView = [[UIView alloc] initWithFrame:CGRectMake(frame.size.width - 20, 12, 8,8)];
            markView.backgroundColor = [UIColor redColor];
            [markView.layer setCornerRadius:4];//圆角
            [self addSubview:markView];
        }
    }
    return self;
}

-(id)initWithFrame:(CGRect)frame title:(NSString *)title imageStr:(NSString *)imageStr leftTopImage:(NSString *)imageLT
{
    self = [super initWithFrame:frame];
    if (self) {
        UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(frame.size.width/2-18, 15, 36, 36)];
        imageView.contentMode = UIViewContentModeRedraw;
        [imageView sd_setImageWithURL:[NSURL URLWithString:imageStr] placeholderImage:[UIImage new]];
        [self addSubview:imageView];
        
        self.titleLable = [[UILabel alloc] initWithFrame:CGRectMake(0, 17+38, frame.size.width, 20)];
        titleLable.text = title;
        titleLable.textAlignment = NSTextAlignmentCenter;
        titleLable.font = [UIFont systemFontOfSize:13];
        titleLable.textColor = [UIColorDispose colorWithHexString:@"505050"];
        [self addSubview:titleLable];
        
        UIImageView *imageLTView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 36, 36)];
        imageLTView.contentMode = UIViewContentModeRedraw;
        imageLTView.image = [UIImage imageNamed:imageLT];
        [self addSubview:imageLTView];
    }
    return self;
}
    
-(id)initWithFrame:(CGRect)frame title:(NSString *)title imageStr:(NSString *)imageStr markBageNumber:(NSInteger)markCount
    {
        self = [super initWithFrame:frame];
        if (self) {
            UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(frame.size.width/2-18, 15, 36, 36)];
            imageView.contentMode = UIViewContentModeRedraw;
            [imageView sd_setImageWithURL:[NSURL URLWithString:imageStr] placeholderImage:[UIImage new]];
            [self addSubview:imageView];
            
            self.titleLable = [[UILabel alloc] initWithFrame:CGRectMake(0, 17+38, frame.size.width, 20)];
            titleLable.text = title;
            titleLable.textAlignment = NSTextAlignmentCenter;
            titleLable.font = [UIFont systemFontOfSize:13];
            titleLable.textColor = [UIColorDispose colorWithHexString:@"505050"];
            [self addSubview:titleLable];
            
            //圆角小图标
            if(markCount > 0){
                NSString *value = [NSString stringWithFormat:@"%ld",markCount];
                CustomBadge *badgeView = [CustomBadge customBadgeWithString:value];
                [self addSubview:badgeView];
                
                CGRect frameBadge = badgeView.frame;
                frameBadge.origin.x = frame.size.width - frameBadge.size.width - 3;
                frameBadge.origin.y = 3;
                badgeView.frame = frameBadge;
            }
            
        }
        return self;
    }

@end
